//Documentation complete
/**
 * @author Jason
 * @version 2009MR20
 */

package ui.internal;

import problemDomain.*;
import control.*;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class for sales report.
 */
public class SalesPerTimeReport extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 6243277202319946869L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml");
		HttpSession session = request.getSession(true);
		if (session != null) {
				String action = request.getParameter("a");
				if (action != null) {
					if (action.equalsIgnoreCase("1"))
						salesPerTimePeriodReport(request, response);
					else if (action.equalsIgnoreCase("2"))
						salesPerTimePeriodReport(request, response);
					else if (action.equalsIgnoreCase("3"))
						salesPerTimePeriodReport(request, response);
				} else
					response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void salesPerTimePeriodReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String from = request.getParameter("fromDate");
		String to = request.getParameter("toDate");
		String periodStr = request.getParameter("timePeriod");
		
		if (from != null && to != null && periodStr != null) {
			PrintWriter out = response.getWriter();
			try {			
				SalesPerTimePeriodReportBean report = new SalesPerTimePeriodReportBean();
			
				SalesTimePeriodReportControl stprc = new SalesTimePeriodReportControl();
				report = stprc.report(from, to, periodStr);
				
				out.write(report.toXML());
			} catch (ErrorBean e) {
				out.write(e.toXML());
			} finally {
				out.close();
			}
		}
	}

	/* END Local Methods */
	
}
